<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;
use Common\Common\Api\Redis\Channel;

/**
 * Class ManyiController
 * @package Pay\Controller
 */
class YemantaobaoController extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $isMobile = isMobile();

        $return = $this->orderadd( $parameter );

        $data = array();
        $data['app_id'] = $this->config['mch_id'];
        $data['price'] = $return['amount'];
        $data['out_order_id'] = $return['orderid'];
        switch($this->config['paytype']) {
            //微信
            case 1:
                $data['type'] = 'wx';
                break;
            case 3:
            case 4:
            case 17:
            case 18:
                $data['type'] = 'alipay';
                break;
            default:
                $this->showmessage('暂不支持此支付方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }
        $data['notifyurl'] = $return['notifyurl'];
        $data['returnurl'] = $return['callbackurl'];
        $data['extend'] = 'XZB';

        $str1 = $data['price'] . $data['out_order_id'] . $data['type'] . $data['notifyurl'] . $data['returnurl'] . $data['extend'];
        $str2 = md5($str1) . $this->config['signkey'];
        $data['sign'] = md5($str2);


        $res = curl_get3($this->config['gateway'], $data);
        $res  = json_decode($res, true);

        //echo "<pre/>"; print_r($res);exit;

        $code = $res['data']['h5url'];

        if(!$code){
            if($res['msg']){
                $this->showmessage($res['msg'],['code'=>C("THIRDCODES.".$this->class)]);
            }

            $this->showmessage('获取支付链接失败!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        if($isMobile){
            header("Location:{$code}"); exit;
        }

        $paytype = transArr( C("PAYTYPES"), "id");
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);
        $this->assign("imgurl", C("website.DOMAIN")."/Pay_Qrcode_getCode?type=2&data=".base64_encode($code));

        switch($this->config['paytype']){
            //支付宝扫码
            case 3:
                //支付宝H5
            case 4:
            case 17:
            case 18:
                if(isMobile()){
                    header("Location:{$code}"); exit;
                }else{
                    $this->display("WeiXin/alipay");
                }
                break;
            default:
                $this->display("WeiXin/Pay");
        }

    }

    //通知
    public function notifyurl(){
		//$this->debug_callback();
        if(!$this->checkip(getIP(),['47.75.120.124'])){
            exit("wrong");
        }
        $p = I("post.");

        //$p = array ( 'out_order_id' => 'sn20190630213143233', 'price' => '500.00', 'type' => 'alipay', 'paytime' => '1561901550', 'extend' => 'XZB', 'sign' => 'b4fce443682bc04a38c1aca548a69297', );

        if(!$p || empty($p)){
            exit("wrong");
        }

        //获取订单信息
        $orderid = $p['out_order_id'];
        $find = $this->checkOrderNotify($orderid);
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['sign'];

        //out_order_id + price + type + paytime + extend)

        $str1 = $p['out_order_id'] . $p['price'] . $p['type'] . $p['paytime'] .$p['extend'];
        $str2 = md5($str1) . $this->config['signkey'];
        $sign2 = md5($str2);

        if($sign !== $sign2){
            exit("sign_wrong");
        }

        $this->async($find['pay_orderid'],"success");
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}